//
//  SmallDecisionsGoogleAdsHelper.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/6/14.
//

#import "SmallDecisionsGoogleAdsHelper.h"

#ifdef DEBUG
#define GoogleAdsLaunchID   @"ca-app-pub-3940256099942544/5662855259"
#define GoogleAdsRewardID  @"ca-app-pub-3940256099942544/1712485313"
#define GoogleAdsInterstitialID  @"ca-app-pub-3940256099942544/4411468910"
#define GoogleAdsBannerID  @"ca-app-pub-3940256099942544/2934735716"

#else

#define GoogleAdsLaunchID   @"ca-app-pub-6708532325679326/6362189715"
#define GoogleAdsRewardID  @"ca-app-pub-6708532325679326/1847769602"
#define GoogleAdsInterstitialID  @"ca-app-pub-6708532325679326/6741210428"
#define GoogleAdsBannerID  @"ca-app-pub-6708532325679326/2568359569"

#endif
@implementation SmallDecisionsGoogleAdsHelper

+ (void)startRegisterGoogleMobileAds {
#ifndef DEBUG
    [[GADMobileAds sharedInstance] startWithCompletionHandler:nil];
#endif
}

// 开屏广告
+ (void)startLoadGoogleLaunchMobileAdsWithController:(UIViewController *)controller
                                        failureBlock:(_Nullable SmallDecisionsGoogleAdsFailureBlock)failureBlock {
    
#ifndef DEBUG
        GADRequest *request = [GADRequest request];
    [GADAppOpenAd loadWithAdUnitID:GoogleAdsLaunchID request:request completionHandler:^(GADAppOpenAd * _Nullable appOpenAd, NSError * _Nullable error) {

            if (error) {

                if (failureBlock) {

                    failureBlock();
                }
    #ifdef DEBUG
                NSLog(@"启动广告加载失败：%@", error.localizedDescription);
    #endif
                return;
            }
            [appOpenAd presentFromRootViewController:controller];
        }];
#endif
}

//插屏广告
+ (void)startLoadGoogleViewMobileAdsInterstitialToController:(UIViewController *)viewController failureBlock:(_Nullable SmallDecisionsGoogleAdsFailureBlock)failureBlock {
#ifndef DEBUG
    if(![SmallDecisionsSingletonHelper shareInstance].isVIP) {
        GADRequest *request = [GADRequest request];
        [GADInterstitialAd loadWithAdUnitID:GoogleAdsInterstitialID
                                      request:request
                            completionHandler:^(GADInterstitialAd *ad, NSError *error) {
            if (error) {

                if (failureBlock) {

                    failureBlock();
                }
        #ifdef DEBUG
                NSLog(@"启动广告加载失败：%@", error.localizedDescription);
        #endif
            } else {
                [ad presentFromRootViewController:viewController];
            }
        }];
    }
#endif
}

//横幅广告
+ (void)startLoadGoogleViewMobileAdsAddBannerViewToController:(UIViewController *)viewController
                                                 failureBlock:(_Nullable SmallDecisionsGoogleAdsFailureBlock)failureBlock {
#ifndef DEBUG
    if(![SmallDecisionsSingletonHelper shareInstance].isVIP) {
        GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeBanner];
        bannerView.translatesAutoresizingMaskIntoConstraints = NO;
        bannerView.adUnitID = GoogleAdsBannerID;
        bannerView.rootViewController = viewController;
        [bannerView loadRequest:[GADRequest request]];
        [viewController.view addSubview:bannerView];
        [bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.centerX.equalTo(viewController.view);
        }];
    }
#endif
}
@end
